#include"bqueue.h"
#include<stdlib.h>

int bqueue_init(bqueue* b_q){
  int rv = 0;
  pthread_mutex_t* lock;

  lock = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
  pthread_mutex_init(lock, NULL);

  b_q->l = lock;
  b_q->q = (queue*)malloc(sizeof(queue));
  queue_init(b_q->q, 1);

  return rv;
}

int bqueue_enqueue(bqueue* b_q, void* item){
  int rv = 0;
  //enter critical region
  pthread_mutex_lock(b_q->l);
  rv = queue_enqueue(b_q->q, item);
  //exit critical
  pthread_mutex_unlock(b_q->l);
  return rv;
}
void* bqueue_dequeue(bqueue* b_q){
  void* item;

  //enter critical region
  pthread_mutex_lock(b_q->l);
  item = queue_dequeue(b_q->q);
  //exit critical
  pthread_mutex_unlock(b_q->l);

  return item;
}
